package com.ld.front.controller.notice;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ld.common.controller.ProjectBasicController;
import com.ld.common.model.json.ApiResult;
import com.ld.common.model.json.ApiResultConverter;
import com.ld.front.util.notice.NoticeSendUtil;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.core.util.notice.WeChatUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 通知服务
 * 
 * @ClassName NoticeController
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2018年12月26日 下午5:35:58
 *
 */
@Slf4j
@Api(tags = "通知服务") // description 必填 文档显示 标题
@RestController
@RequestMapping("${shieldsb.frontPath}/notice")
public class NoticeController extends ProjectBasicController<Object> {

    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "发送邮件通知（支持带附件）", notes = "传入标题内容接收人附件") // value 必填文档显示
    @RequestMapping(value = "/sendemail", method = { RequestMethod.POST }) // method 必填 否则文档会很冗余
    public ApiResult sendemailWithAttach(
            @ApiParam(name = "mailFromName", value = "发送方的显示名称（非发送方邮箱地址）") @RequestParam(required = false) String mailFromName,
            @ApiParam(name = "emailTo", value = "接收邮箱", required = true) @RequestParam() String emailTo,
            @ApiParam(name = "subject", value = "主题", required = true) @RequestParam() String subject,
            @ApiParam(name = "content", value = "内容", required = true) @RequestParam() String content,
            @ApiParam(name = "attachfiles", value = "附件文件（数组，单个文件限制10M）") @RequestParam(value = "attachfiles", required = false) MultipartFile[] files,
            @ApiParam(name = "attachUrls", value = "附件url（数组）") @RequestParam(value = "attachUrls", required = false) String[] attachUrls) {

        Result result = NoticeSendUtil.sendEmail(mailFromName, emailTo, subject, content, files, attachUrls);
        String flag = "发送邮件失败！";
        return ApiResultConverter.result2ApiResult(result, flag);
    }

    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "发送短信通知", notes = "传入标题内容接收人") // value 必填文档显示
    @RequestMapping(value = "/sendsms", method = { RequestMethod.POST }) // method 必填 否则文档会很冗余
    public ApiResult sendsms(@ApiParam(name = "phoneNum", value = "手机号", required = true) @RequestParam() String phoneNum,
            @ApiParam(name = "message", value = "发送内容", required = true) @RequestParam() String message,
            @ApiParam(name = "signName", value = "签名") @RequestParam(required = false) String signName) {
        Result smsResult = NoticeSendUtil.sendSMS(phoneNum, message, signName);
        String msg = "发送异常！";
        return ApiResultConverter.result2ApiResult(smsResult, msg);
    }

    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "发送企业微信通知", notes = "") // value 必填文档显示
    @RequestMapping(value = "/sendepwechat", method = { RequestMethod.POST }) // method 必填 否则文档会很冗余
    public ApiResult sendEpWeChat(@ApiParam(name = "corpid", value = "corpid", required = true) @RequestParam() String corpid,
            @ApiParam(name = "corpsecret", value = "corpsecret", required = true) @RequestParam() String corpsecret,
            @ApiParam(name = "agentid", value = "agentid", required = true) @RequestParam() String agentid,
            @ApiParam(name = "sendTo", value = "接收人账号", required = true) @RequestParam() String sendTo,
            @ApiParam(name = "msg", value = "内容", required = true) @RequestParam() String msg) {
        Result result = WeChatUtil.sendEnterpriseWeChatMsg(corpid, corpsecret, agentid, sendTo, msg);
        log.warn(result + "");
        String flag = "发送异常！";
        return ApiResultConverter.result2ApiResult(result, flag);
    }

}
